package com.cyz.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cyz.pojo.Orders;
import com.cyz.service.OrdersService;
import com.cyz.service.ProductService;
import com.cyz.service.ShopcartService;
import com.cyz.service.UserService;
import com.cyz.service.impl.OrdersServiceImpl;
import com.cyz.service.impl.ProductServiceImpl;
import com.cyz.service.impl.ShopcartServiceImpl;
import com.cyz.service.impl.UserServiceImpl;


@Controller
@RequestMapping("/Checkorder")
public class Checkorder {
	@RequestMapping("/checkorder")
	public ModelAndView checkorder(HttpServletRequest request,HttpServletResponse response)
	{
		ModelAndView mv=new ModelAndView();

		String productName=request.getParameter("productName");
		Integer amount=Integer.parseInt((String)request.getParameter("amount"));
		ProductService productService=new ProductServiceImpl();
		
		com.cyz.pojo.Product product=productService.getProductByProductName(productName);
		
		//float total=Float.parseFloat(request.getParameter("total")) ;
		mv.addObject("amount",amount);
		mv.addObject("product",product);
		mv.addObject("total",product.getPrice()*amount);
		mv.setViewName("checkorder");
		mv.addObject("prePage",request.getSession().getAttribute("nowPage"));
		return  mv;
		
		
	}
	/**
	  * 结算订单
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("/settlement")
	public void settlement(HttpServletRequest request,HttpServletResponse response) throws IOException{
		PrintWriter printWriter =  response.getWriter();	
		Date date = new Date();//获取当前时间
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd:hh:mm:ss");
		dateFormat.format(date);
//		System.out.println(request.getParameter("total"));
//		float total=Float.parseFloat(request.getParameter("total")) ;
//		
		String productName = request.getParameter("productName");//获取产品ID
	//	System.out.println(productName);
		ProductService productservice=new ProductServiceImpl();
		Integer productId=productservice.getProductIdByName(productName);
	//	System.out.println("productID:"+productId);
		String userName=(String) request.getSession().getAttribute("userName");
		UserService userService=new UserServiceImpl();
		Integer userId=userService.getUserIdByUserName(userName);//获取用户id
	//	System.out.println("userID:"+userId);
		Integer amount = Integer.parseInt(request.getParameter("amount"));//获取产品数量
	//	System.out.println("amount:"+amount);
		printWriter.write("{\"res\":\"1\"}");
		//float price=Float.parseFloat(request.getParameter("price")) ;
		//float total=price*amount;
		//System.out.println("price:"+price);
		float total=Float.parseFloat(request.getParameter("total")) ;
	//	System.out.println("total:"+total);
		Orders orders=new Orders();
		orders.setOrderTime(date);
		orders.setAmount(amount);
		orders.setUserId(userId);
		orders.setProductId(productId);
		orders.setSumPrice(total);
		
		OrdersService orderService =new OrdersServiceImpl();
	    orderService.addOrder(orders);
		
		printWriter.close();
	}
	
}
